// pages/details/details.js
const app = getApp()
var url = require('../../utils/url.js');
var goodsId;
var token;
var user_id = 0;
var formid;
var shareuSer = 0;

Page({
  /**
   * 页面的初始数据
   */
  data: {
    banner: [],
    indicatorDots: true,  //小点
    indicatorColor: "white",
    autoplay: true,  //是否自动轮播
    interval: 3000,  //间隔时间
    duration: 500,  //滑动时间
    tabs: ["商品详情", "视频"],
    activeIndex: 0,
    thisTimeItem: [
      { thunb: "../../images/details/tx.png", user: "杨某某", cishu: "5", time: "10:23:25" },
      { thunb: "../../images/details/tx.png", user: "杨某某", cishu: "5", time: "10:23:25" },
      { thunb: "../../images/details/tx.png", user: "杨某某", cishu: "5", time: "10:23:25" },
      { thunb: "../../images/details/tx.png", user: "杨某某", cishu: "5", time: "10:23:25" },
      { thunb: "../../images/details/tx.png", user: "杨某某", cishu: "5", time: "10:23:25" },
      { thunb: "../../images/details/tx.png", user: "杨某某", cishu: "5", time: "10:23:25" },
      { thunb: "../../images/details/tx.png", user: "杨某某", cishu: "5", time: "10:23:25" }
    ],
    goodsbody: "",
    image:"",
    num:1,
    spec:true,
    formid:"",
    goods_storage:"",
    jifen:"",
    row:""
  },
  tabClick: function (e) {
    var that = this;
    var idx = e.currentTarget.dataset.idx;
    var activeIndex = that.data.activeIndex;
    if (idx == activeIndex) {
      return false
    } else {
      that.setData({
        activeIndex: idx
      })
    }
  },
  bindMinus: function () {
    var num = this.data.num;
    if (num > 1) {
      num--;
    }
    // 将数值与状态写回
    this.setData({
      num: num,
    });
  },
  /* 点击加号 */
  bindPlus: function () {
    var num = this.data.num;
    var goods_storage = this.data.goods_storage;
    // var limit_num = this.data.limit_num;
    if (num < goods_storage) {
      num++;
    } else {
      wx.showToast({
        title: '不能再大了',
        icon: 'none',
      })
      
    }
   
    console.log(num)
    
    // 将数值与状态写回
    this.setData({
      num: num,
    });
  },
  getGood() {
    var that = this;
    wx.showLoading({
      title: '加载中..',
    })
    wx.request({
      url: url.URl + 'goods/exchange/info',
      data: { goods_id: goodsId },
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var msg = res.data.msg;
        if (res.data.code == 200) {
          var row=res.data.data.row;
          console.log(res)
          that.setData({
            banner: row.goods_images,
            row: row,
            ads:res.data.data.ads,
            goodsbody: row.goods_body,
            goods_storage: row.goods_storage
          })
          wx.hideLoading()
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  tanSpec:function(e){
    var that = this;
    token = wx.getStorageSync("token")
    formid = e.detail.formId;
    this.setData({
      formid: formid
    })
    if (token) {
      that.getJifen() 
      this.setData({
        spec: false
      })
    } else {
      wx.getStorage({
        key: 'rec_id',
        success: function (res) {
          shareuSer = res.data
          app.onGotUserInfo(e, that, formid, shareuSer)
        },
        fail: function () {
          shareuSer = 0
          app.onGotUserInfo(e, that, formid, shareuSer)
        }
      })
    }
  },
  close:function(){
    this.setData({
      spec: true
    })
  },
  getJifen() {
    var that = this;
    wx.request({
      url: url.URl + 'my/jifen',
      data: {},
      method: 'get',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res)
        var msg = res.data.msg
        if (res.data.code == 200) {
          that.setData({
            jifen: res.data.data
          })
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    goodsId = options.goodsId;
    if (options.userId) {
      wx.setStorage({
        key: 'rec_id',
        data: options.userId,
      })
    }
    that.getGood()
  },
  imageLoad: function (e) {
    var $width = e.detail.width,    //获取图片真实宽度
      $height = e.detail.height,
      ratio = $width / $height;    //图片的真实宽高比例
    var viewWidth = 750,           //设置图片显示宽度，左右留有16rpx边距
      viewHeight = 750 / ratio;    //计算的高度值
    var image = this.data.goodsbody;
    //将图片的datadata-index作为image对象的key,然后存储图片的宽高值
    image[e.target.dataset.index] = {
      width: viewWidth,
      height: viewHeight
    }
    // console.log(image)
    this.setData({
      image: image
    })
  },
  toExchange:function(){
      var that=this;
      var data=[];
      console.log(data)
      var row = that.data.row;
      row.goods_num = that.data.num
      data.push(row);
      var jsonselGoods = JSON.stringify(data)
      wx.navigateTo({
        url: '/pages/ConfirmOrder/ConfirmOrder?goodsInfo=' + jsonselGoods + '&type=2',
      })
  },
  toIndex:function(){
     wx.switchTab({
       url: '/pages/index/index',
       success: function(res) {},
       fail: function(res) {},
       complete: function(res) {},
     })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
     wx.getStorage({
       key: 'userinfo',
       success: function(res) {
         user_id=res.data.id
       },
     })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var that = this;
    var row = that.data.row;
    console.log(row)
    if (res.from === 'button') {

    }
    return {
      title: row.goods_name,
      path: '/pages/LkDetails/LkDetails?goodsId=' + row.id + '&userId=' + user_id,
      imageUrl: row.goods_thumb,
      success: function (res) {
        console.log('成功', res)
      }
    }
  }
})